package com.ztools.pc.service.impl.coupon;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ztools.common.model.pc.coupon.CouponTemplate;
import com.ztools.common.model.pc.coupon.CouponUsed;
import com.ztools.common.model.pc.information.WeChatMember;
import com.ztools.common.util.common.PageUtil;
import com.ztools.common.util.pc.DictUtil;
import com.ztools.pc.mapper.coupon.CouponUsableMapper;
import com.ztools.pc.service.coupon.CouponTemplateService;
import com.ztools.pc.service.coupon.CouponUsedService;
import com.ztools.pc.service.information.MemberService;
import com.ztools.pc.service.sys.DictService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: seeTools
 * @Date: 2023/5/18  12:10
 * @Version 1.0
 */
@Service
public class CouponUsedServiceImpl extends ServiceImpl<CouponUsableMapper, CouponUsed> implements CouponUsedService {

    @Resource
    private CouponTemplateService couponTemplateService;
    @Resource
    private DictService dictService;
    @Resource
    private MemberService memberService;


    @Override
    public Page<CouponUsed> findList(PageUtil<CouponUsed> pageUtil) {
        List<OrderItem> orders = new ArrayList<>();
        QueryWrapper<CouponUsed> queryWrapper = getQueryWrapper(pageUtil.getParams());
        Page<CouponUsed> pages = new Page<>(pageUtil.getPage(), pageUtil.getSize());
        // 排序数据  StrUtil.toUnderlineCase huTools将驼峰装换为下划线格式
        orders.add(new OrderItem(StrUtil.toUnderlineCase(pageUtil.getSortName()), "asc".equals(pageUtil.getSortOrder())));
        pages.setOrders(orders);
        Page<CouponUsed> page = page(pages, queryWrapper);
        dictionary(page.getRecords());
        return page;
    }


    @Override
    public List<CouponUsed> findByObject(CouponUsed couponUsed) {
        QueryWrapper<CouponUsed> queryWrapper = new QueryWrapper<>(couponUsed);
        return list(queryWrapper);
    }

    public QueryWrapper<CouponUsed> getQueryWrapper(CouponUsed couponUsed) {
        QueryWrapper<CouponUsed> queryWrapper = new QueryWrapper<>();
        if (couponUsed.getMemberId() != null) {
            queryWrapper.lambda().eq(CouponUsed::getMemberId, couponUsed.getMemberId());
        }
        if (couponUsed.getTemplateId() != null) {
            queryWrapper.lambda().eq(CouponUsed::getTemplateId, couponUsed.getTemplateId());
        }
        if (couponUsed.getCouponType() != null) {
            queryWrapper.lambda().eq(CouponUsed::getCouponType, couponUsed.getCouponType());
        }
        if (couponUsed.getStartDate() != null) {
            queryWrapper.lambda().le(CouponUsed::getUseTime, couponUsed.getEndDate());
        }
        if (couponUsed.getEndDate() != null) {
            queryWrapper.lambda().gt(CouponUsed::getUseTime, couponUsed.getStartDate());
        }
        return queryWrapper;
    }

    /**
     * 字典翻译
     *
     * @param couponUsedList 对象集合
     */
    @Override
    public void dictionary(List<CouponUsed> couponUsedList) {
        for (CouponUsed couponUsed : couponUsedList) {
            dictionary(couponUsed);
        }
    }

    public void dictionary(CouponUsed couponUsed) {
        if (couponUsed != null) {
            if (couponUsed.getTemplateId() != null) {
                CouponTemplate template = couponTemplateService.getById(couponUsed.getTemplateId());
                couponUsed.setTemplateName(template.getName());
                couponUsed.setAmountMeet(template.getAmountMeet());
                couponUsed.setDiscountAmount(template.getDiscountAmount());
                couponUsed.setDaysLimit(template.getDaysLimit());
                couponUsed.setBackground(template.getBackground());
            }
            if (couponUsed.getCouponType() != null) {
                couponUsed.setCouponTypeName(dictService.findByTypeCode(DictUtil.COUPON_TYPE, couponUsed.getCouponType() + ""));
            }
            if (StrUtil.isNotEmpty(couponUsed.getUseRoute())) {
                couponUsed.setUseRouteName(dictService.findByTypeCode(DictUtil.COUPON_USE_ROUTE, couponUsed.getUseRoute()));
            }
            if (couponUsed.getMemberId() != null) {
                WeChatMember member = memberService.getById(couponUsed.getMemberId());
                couponUsed.setMemberName(member.getNickName());
            }
        }
    }
}
